package com.fafu.control;

import com.fafu.model.Manager;
import com.fafu.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import util.Dictionary;
import util.JsonRsp;
import util.MyMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Api("用户相关接口")
@Controller
@RequestMapping("/user")
public class UserControl {
    private Logger logger = LoggerFactory.getLogger(UserControl.class);
    @Autowired
    private UserService userService;

    @ApiOperation("登陆")
    @ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public JsonRsp login(HttpServletRequest request, HttpServletResponse response
            , @RequestParam(value = "iden")@ApiParam("账号") String iden
            , @RequestParam(value = "password")@ApiParam("密码") String password
            , @RequestParam(value = "type")@ApiParam("类别：学生0，管理员1") int type){
        try{
            Map map = userService.loggin(new MyMap()
                    .put("iden",iden)
                    .put("password",password)
                    .put("type",type)
                    .put("request",request)
                    .put("response",response).toMap());
            if((Integer)map.get("code") != Dictionary.CODE.SUCCESS)
                return JsonRsp.fail((Integer)map.get("code"),(String)map.get("msg"));
            return JsonRsp.success(map);
        }catch (Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }

    @ApiOperation("修改密码")
    @ResponseBody
    @RequestMapping(value = "/passwordChange",method = RequestMethod.POST)
    public JsonRsp passworChange(HttpServletRequest request, HttpServletResponse response
            ,@RequestParam(value = "id")@ApiParam("用户id") Integer id
            ,@RequestParam(value = "type")@ApiParam("type 0 :学生 1：管理员") Integer type
            ,@RequestParam(value = "password")@ApiParam("密码") String password
            ,@RequestParam(value = "oldPassword")@ApiParam("旧密码") String oldPassword){
        try{
            Map map = userService.pwdChange(new MyMap()
                    .put("id",id)
                    .put("type",type)
                    .put("password",password)
                    .put("oldPassword",oldPassword)
                    .put("request",request)
                    .put("response",response).toMap());
            if((Integer)map.get("code") != Dictionary.CODE.SUCCESS)
                return JsonRsp.fail((Integer)map.get("code"),(String)map.get("msg"));
            return JsonRsp.success(map);
        }catch (Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }
    @ApiOperation("登出")
    @ResponseBody
    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    public JsonRsp logout(HttpServletRequest request, HttpServletResponse response){
        try{
            userService.loggout(request,response);
            return JsonRsp.success("");
        }catch (Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }
    @ApiOperation("获得用户id")
    @ResponseBody
    @RequestMapping(value = "/getid",method =RequestMethod.POST)
    public JsonRsp getid(HttpServletRequest request){
        try{
            Map map = userService.getUserId(request);
            return JsonRsp.success(map);
        }catch (Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }

    @ApiOperation("获得管理员个人信息")
    @ResponseBody
    @RequestMapping(value = "/getUserInfo",method =RequestMethod.GET)
    public JsonRsp getUserInfo(
            @RequestParam(value = "id")@ApiParam("管理员id") Integer id){
        try{
            return JsonRsp.success(userService.getUserInfo(id).get("data"));
        }catch (Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }
    @ApiOperation("获得管理员个人信息")
    @ResponseBody
    @RequestMapping(value = "/getUserInfoModel",method =RequestMethod.GET)
    public JsonRsp getUserInfoModel(
            @RequestParam(value = "id")@ApiParam("管理员id") Integer id){
        try{
            return JsonRsp.success(userService.getUserInfoModel(id));
        }catch (Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(Dictionary.CODE.SYSTEM_ERROR,"系统内部错误");
        }
    }
}
